package gucheng;

import java.util.*;
import java.util.regex.Matcher;

/**
 * @Classname MeetingRooms
 * @Description TODO
 * @Date 2021/12/30 20:20
 * @Created by ASUS
 */
public class MeetingRooms {
    // 给一个二维数组，里面是每场会议的起始时间，判断一个人是否可以参加所有的会议
    // example: input [[0,30],[5,10],[15,20]]
    //          output: false
    public boolean canAttendAllMeetings(int[][] intervals) {
        if (intervals.length <= 1) return true;// if there is less than one meeting, definitely true
        // 按照会议的开始时间进行排序，只要intervals里面的元素没有重叠的部分即为可以同时参加，反之立即返回false
        Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
        System.out.println(Arrays.deepToString(intervals));
        int[] cur = intervals[0];
        for (int i = 1; i < intervals.length; i++) {
            if (cur[1] > intervals[i][0]) {
                return false;
            }
        }
        return true;
    }

    public int minimumMeetingRooms(int[][] intervals) {
        // 至多只有一个会议，可以直接得到结果，返回即可
        if (intervals.length <= 1) return intervals.length;
        // time - weight
        List<int[]> t_w = new ArrayList<>();
        for (int[] interval : intervals) {
            t_w.add(new int[]{interval[0], 1});
            t_w.add(new int[]{interval[1], -1});
        }
        t_w.sort(Comparator.comparingInt(a -> a[0]));
        t_w.forEach(a -> System.out.print(Arrays.toString(a) + " ,"));
        int cnt = 0, res = 0;
        for (int[] xy : t_w) {
            int y = xy[1];
            cnt += y;
            res = Math.max(cnt, res);
        }
        return res;
    }

    public int[][] merge(int[][] intervals) {
        if (intervals.length <= 1) return intervals;
        Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
        int[] cur = intervals[0];
        List<int[]> ans = new ArrayList<>();
        ans.add(cur);
        for (int[] next : intervals) {
            if (cur[1] >= next[0]) {
                // overlap
                cur = new int[]{Math.min(cur[0], next[0]), Math.max(cur[1], next[1])};
                ans.set(ans.size() - 1, cur);
            } else {
                ans.add(next);
                cur = next;
            }
        }
        return ans.toArray(new int[ans.size()][2]);
    }

    public int[][] insert(int[][] intervals, int[] newInterval) {
        List<int[]> ans = new ArrayList<>();
        for (int[] cur : intervals) {
            if (newInterval == null || cur[1] < newInterval[0]) {// 当前的interval的终止时间早于新的开始时间
                ans.add(cur);// 直接添加到结果集中即可
            } else if (newInterval[1] < cur[0]) {// 新的结束时间早于当前interval的开始时间
                ans.add(newInterval);
                ans.add(cur);
                newInterval = null;
            } else {
                // 这两个interval有重叠的部分，merge一下
                newInterval = merge2(cur, newInterval);
            }
        }
        if (newInterval != null) ans.add(newInterval);
        return ans.toArray(new int[ans.size()][]);
    }


    private int[] merge2(int[] a, int[] b) {
        return new int[]{Math.min(a[0], b[0]), Math.max(a[1], b[1])};
    }


    public static void main(String[] args) {
        System.out.println(new MeetingRooms().canAttendAllMeetings(new int[][]{
                {0, 30}, {15, 20}, {5, 10}
        }));
        System.out.println(new MeetingRooms().minimumMeetingRooms(new int[][]{
                {0, 30}, {15, 20}, {5, 10}
        }));
    }
}
